package com.GameInterfaceDevelopment;
//选择用户所喜欢的城市
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//单选按钮JRadioButton
public class JRadioButtonTest {
    JFrame f = null;
    public  JRadioButtonTest(){
        //创建一个JFrame窗口对象f
        f = new JFrame("单选框示例");
        //创建一个内容面板容器
        Container contentPane = f.getContentPane();
        //设置该窗口的布局格式为流布局
        contentPane.setLayout(new FlowLayout());

        //创建一个面板对象p1
        JPanel p1 = new JPanel();
        //设置布局管理器的格式
        p1.setLayout(new GridLayout(1,3));
        p1.setBorder(BorderFactory.createTitledBorder("选择你喜欢的城市"));
        //定义3个JRadioButton单选按钮
        JRadioButton r1 = new JRadioButton("北京");
//        JRadioButton r1 = new JRadioButton();
        JRadioButton r2 = new JRadioButton("上海");
        JRadioButton r3 = new JRadioButton("山西");


        //将显示文本添加到面板对象P1中去
        p1.add(r1);
        p1.add(r2);
        p1.add(r3);
        //设置”北京“单选按钮的状态为被选中
//        r1.setSelected(true);
        //面板对象p1加到窗口内容面板容器中
        contentPane.add(p1);

        f.setLocationRelativeTo(null);
        f.setSize(300,200);
        //窗口大小
        //f.pack();
        //窗口可视化
        f.setVisible(true);
        //添加一个窗口监听器
 /*       f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });*/
    }

    //main方法中调用构造方法JRadioButtonTest()
    public static void main(String[] args) {
        new JRadioButtonTest();
    }
}
